package net.lwf.handler.math.arithmatics;

import java.util.List;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.entities.math.SumUtil;
import lwf.handler.helper.DisplayFormatter;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.number.util.RandomNumberGenerator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;


public class BasicMathProcessHandler extends ProcessHandler implements IProcessHandler {
	
	private int param_one = 0;
	private int param_two = 0;
	private int answer = 0;
	
	private String operation;
	
	public BasicMathProcessHandler(String operation) {
		this.operation = operation;
		this.init();
	}

	public DisplayFormatter getDisplayFormatter() {
		return null;
	}

	public String getQuestionDesc() {
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return this.generateSingle(false);
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		return null;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
	}

	@Override
	public int getRows() {
		return 0;
	}

	@Override
	public int getColumns() {
		return 0;
	}

	@Override
	public boolean isPortrait() {
		return false;
	}

	@Override
	public String getPdfInstructionalText() {
		return null;
	}

	@Override
	public String getPdfNote() {
		return null;
	}
	
	//---------------------------------------------------------------------------------------------------
	// Private methods
    //---------------------------------------------------------------------------------------------------	
	private QuestionInfo generateSingle(boolean isMultiple) {
		QuestionInfo qinfo = new QuestionInfo();
		qinfo.setAnswer(String.valueOf(answer));
		qinfo.setHtmlQuestion(this.generateHtmlQuestion());
		qinfo.setPlaceHolder(this.getInstructionText());
		return qinfo;
	}
	
	private void init() {
		int start = 1;
		int end = 10;
		param_one = RandomNumberGenerator.generate(start, end);
		param_two = RandomNumberGenerator.generate(start, end);
		
		if(this.operation.equals(IHandlerConstant.ADDITION)) {
			while(param_one == param_two) {
				param_two = RandomNumberGenerator.generate(start, end);
			}
			this.answer = param_one + param_two;
		}
		else if(this.operation.equals(IHandlerConstant.SUBTRACTION)){
			//for subtraction
			while(param_one < param_two || param_one == param_two) {
				param_two = RandomNumberGenerator.generate(start, end);
			}
			this.answer = param_one - param_two;
		}
		else if(this.operation.equals(IHandlerConstant.COUNT)){
			//for counting
			param_one = RandomNumberGenerator.generate(1, 20);
			this.answer = param_one;
		}		
	}
	
	private String generateHtmlQuestion() {
		if(this.operation.equals(IHandlerConstant.COUNT)) {
			return this.generateCountTable();
		}
		return this.generateAddSubtract();
	}
	
	private String generateAddSubtract() {
		String imageNum = String.valueOf(RandomNumberGenerator.generate(1,20));
		StringBuffer sbQBlock = new StringBuffer();
		sbQBlock.append("<table width=\"80%\" id=\"lwfborder\" border=\"1\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\">"); //table 2
			sbQBlock.append("<tr>");
				for(int i=0; i<param_one; i++) {
					sbQBlock.append("<td class=\"datacenter\">");
					String imagename = "image/math/show/showpic_" + imageNum + ".gif";
					sbQBlock.append("<img src=\"" + imagename + "\">");
					sbQBlock.append("</td>");
				}
				sbQBlock.append("<td class=\"datacenter\">");
				sbQBlock.append(SumUtil.getOperationImageHtml(this.operation));
				sbQBlock.append("</td>");
				for(int j=0; j<param_two; j++) {
					sbQBlock.append("<td class=\"datacenter\">");
					String imagename = "image/math/show/showpic_" + imageNum + ".gif";
					sbQBlock.append("<img src=\"" + imagename + "\">");
					sbQBlock.append("</td>");
				}
				sbQBlock.append("<td class=\"datacenter\">");
				sbQBlock.append("=");
				sbQBlock.append("</td>");		
				sbQBlock.append("<td class=\"question\">");
				sbQBlock.append("?");
				sbQBlock.append("</td>");			
				sbQBlock.append("</tr>");
				sbQBlock.append("<tr>");
				sbQBlock.append("<td colspan=\"" + (param_one + param_two + 3) + "\">&nbsp;</td>");
				sbQBlock.append("</tr>");
				sbQBlock.append("<tr>");
				sbQBlock.append("<td class=\"datacenter\" colspan=\"" + param_one + "\">");
				sbQBlock.append(param_one);
				sbQBlock.append("</td>");
				sbQBlock.append("<td class=\"datacenter\">");
				sbQBlock.append(SumUtil.getOperationImageHtml(this.operation));
				sbQBlock.append("</td>");	
				sbQBlock.append("<td class=\"datacenter\" align=\"left\" colspan=\"" + param_two + "\">");
				sbQBlock.append(param_two);
				sbQBlock.append("</td>");	
				sbQBlock.append("<td class=\"datacenter\">");
				sbQBlock.append("=");
				sbQBlock.append("</td>");							
				sbQBlock.append("<td class=\"question\">");
				sbQBlock.append("?");
				sbQBlock.append("</td>");		
		    sbQBlock.append("</tr>");
		sbQBlock.append("</table>");

		return sbQBlock.toString();
	}
	
	private String generateCountTable() {
		String imageNum = String.valueOf(RandomNumberGenerator.generate(1,20));
		StringBuffer sbQBlock = new StringBuffer();
		sbQBlock.append("<table width=\"80%\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\">"); //table 2
        	sbQBlock.append("<tr>");
				sbQBlock.append("<td class=\"datacenter\">");
					sbQBlock.append("<table border=\"0\" align=\"center\" cellspacing=\"2\" cellpadding=\"0\">");
					boolean countdisplay = false;
					int nCounter = 0;
			        while(!countdisplay) {
			        	sbQBlock.append("<tr>");
				        	for(int i=0; i<10; i++) {
				        		sbQBlock.append("<td>");
				        		    if((answer != i + nCounter) && !countdisplay ) {
										String imagename = "image/math/show/showpic_" + imageNum + ".gif";
										sbQBlock.append("<img class=\"lwf_image\" src=\"" + imagename + "\">");	
				        		    }
				        		    else {
				        		    	sbQBlock.append("&nbsp;");
				        		    	countdisplay = true;
				        		    }
				        		sbQBlock.append("</td>");
				        		sbQBlock.append("<td>");
				        			sbQBlock.append("&nbsp;");
				        		sbQBlock.append("</td>");
				        	}
				        	nCounter = nCounter + 10;
			        	sbQBlock.append("</tr>");
			        }
			        sbQBlock.append("</table>");
	            sbQBlock.append("</td>");
	        sbQBlock.append("</tr>");
	    sbQBlock.append("</table>");

		return sbQBlock.toString();	
	}
	
	private String getInstructionText() {
		String text = "Solve the following ";
		if(this.operation.equalsIgnoreCase(IHandlerConstant.ADDITION)) {
			return text += IHandlerConstant.ADDITION + " sum.";
		}
		else if(this.operation.equalsIgnoreCase(IHandlerConstant.SUBTRACTION)) {
			return text += IHandlerConstant.SUBTRACTION + " sum.";
		}
		
		
		return "Count the following pictures.";
	}

/*    private int genData1 = 0;
    private int genData2 = 0;
    private FormatAttribute format;
    
	public BasicMathProcessHandler(String operation) {
		setOperation(operation);
		this.init();
	}
	
	*//**
	 * @see net.lwf.handler.IProcessHandler#getDisplayFormatter()
	 *//*
	public DisplayFormatter getDisplayFormatter() {
		format = new FormatAttribute();
		format.setQuestionNumber(getQuestionNum());
		format.setQuestionTable(this.generateQuestionTable());
		format.setQuestionType(2); 
		
		return new DisplayFormatter(format);
	}

	public String getQuestionDesc() {
		StringBuffer sbQ = new StringBuffer();
		if(getOperation().equals(IHandlerConstant.COUNT)) {
			sbQ.append("Pictures for count = " + getAnswer());
		}
		else {
			sbQ.append(getData1());
			sbQ.append(" ");
			sbQ.append(this.getOperationsign());
			sbQ.append(" ");	
			sbQ.append(getData2());
			sbQ.append(" ");
			sbQ.append(" = ");			
		}

		return sbQ.toString();
	}

	//---------------------------------------------------------------------------------------------------
	// Private methods
    //---------------------------------------------------------------------------------------------------	
	
	private void init() {
		int start = 1;
		int end = 10;
		genData1 = RandomNumberGenerator.generate(start, end);
		genData2 = RandomNumberGenerator.generate(start, end);
		
		if(getOperation().equals(IHandlerConstant.ADDITION)) {
			while(genData1 == genData2) {
				genData2 = RandomNumberGenerator.generate(start, end);
			}
			setAnswer(String.valueOf(genData1 + genData2));
		}
		else if(getOperation().equals(IHandlerConstant.SUBTRACTION)){
			//for subtraction
			while(genData1 < genData2 || genData1 == genData2) {
				genData2 = RandomNumberGenerator.generate(start, end);
			}
			setAnswer(String.valueOf(genData1 - genData2));
		}
		else if(getOperation().equals(IHandlerConstant.COUNT)){
			//for counting
			genData1 = RandomNumberGenerator.generate(1, 20);
			setAnswer(String.valueOf(genData1));
		}		
		
		setData1(String.valueOf(genData1));
		setData2(String.valueOf(genData2));
	}
	
	public String generateQuestionTable() {
		if(getOperation().equals(IHandlerConstant.COUNT)) {
			return this.generateCountTable();
		}

		return this.generateAddSubtract(); //Default
	}
	
	*//**
	 * @param sbQBlock
	 * @param imageNum
	 *//*


	//------------------------------------------------ Methods for PDF generation on the browser
	public void generate(Document document) {
	
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return null;
	}

	@Override
	public int getColumns() {
		return 0;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
	}

	@Override
	public int getRows() {
		return 0;
	}

	@Override
	public boolean isPortrait() {
		return false;
	}

	@Override
	public String getPdfInstructionalText() {
		return null;
	}

	public boolean matchAnswer(String useranswer, AttemptedQuestion aq) {
		return false;
	}

	@Override
	public String getPdfNote() {
		return null;
	}*/

}
